<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="85px"
    >
      <el-form-item label="策略名称" prop="strategyName">
        <ns-input
          v-model="queryParams.strategyName"
          class="!w-240px"
          clearable
          placeholder="请输入策略名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" v-hasPermi="['amp:strategy-testing:search']" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" v-hasPermi="['amp:strategy-testing:search']" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <div class="handle-btns">
      <el-dropdown>
        <el-button type="primary">新增</el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="handleUpdate" v-hasPermi="['amp:strategy-testing:add']"
              >新增测试策略</el-dropdown-item
            >
            <el-dropdown-item @click="handleImport" v-hasPermi="['amp:strategy-testing:import']"
              >从打标策略中导入</el-dropdown-item
            >
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <el-table v-loading="loading" :data="list" row-key="id" show-overflow-tooltip>
      <el-table-column type="index" width="80" label="序号" align="center" />
      <el-table-column prop="strategyName" label="策略名称" align="center" />
      <el-table-column prop="strategyDesc" label="描述" align="center" />
      <el-table-column prop="createTime" label="创建时间" align="center" />
      <el-table-column label="操作" width="230" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleRenew(scope.row.id)"
            v-hasPermi="['amp:strategy-testing:renew']"
          >
            更新
          </el-button>
          <el-button
            link
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['amp:strategy-testing:update']"
          >
            编辑策略
          </el-button>
          <el-button
            link
            type="primary"
            @click="handleUpdate(scope.row, true)"
            v-hasPermi="['amp:strategy-testing:detail']"
          >
            查看
          </el-button>
          <el-button
            link
            type="danger"
            @click="delList(scope.row.id)"
            v-hasPermi="['amp:strategy-testing:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.page"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>
  <ImportTestScheme
    v-model:visible="testSchemeVisible"
    @success="getList"
    v-if="testSchemeVisible"
  />
</template>

<script setup lang="ts">
import { Plus } from '@element-plus/icons-vue'
import { strategyPage, strategyDelete, strategyRenew } from '@/api/strategyManage/strategy/index'
import ImportTestScheme from './component/ImportTestScheme.vue'

const message = useMessage() // 消息弹窗
const queryFormRef = ref() // 搜索的表单
const total = ref(10) // 列表的总页数
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据

const testSchemeVisible = ref(false)
const queryParams = reactive({
  page: 1,
  pageSize: 10,
  strategyName: '',
  type: 'secondary'
})

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.page = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
// const formRef = ref<InstanceType<typeof MaterialForm>>()
// const openForm = () => {
//   formRef.value?.open('create')
// }

function handleImport() {
  testSchemeVisible.value = true
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const { count, data } = await strategyPage(toRaw(queryParams))
    list.value = data
    total.value = count
  } finally {
    loading.value = false
  }
}
getList()
const { proxy } = getCurrentInstance()
/** 添加/修改操作 */
const handleUpdate = (row = {}, isDetail = false) => {
  // dialogTitle.value = type ? (type === 1 ? '修改' : '详情') : '新增'
  // detailId.value = id
  // dialogVisible.value = true
  proxy?.$Drawer({
    params: {
      ...row,
      isDetail: isDetail,
      callBack: () => {
        getList()
      }
    },
    title: row.id ? (isDetail ? `策略详情` : `修改策略`) : `新增策略`,
    size: '70%',
    component: import('./component/SchemeEdit.vue')
  })
}

/** 更新操作 */
const handleRenew = (id = {}, isDetail = false) => {
  proxy?.$modal
    .confirm(`确定要更新吗？`)
    .then(() => {
      strategyRenew(id).then(() => {
        message.success('更新成功')
      })
    })
    .catch(() => {})
}

// 删除
const delList = (id) => {
  proxy?.$modal
    .confirm(`确定要删除吗？`)
    .then(() => {
      strategyDelete(id).then(() => {
        getList()
        message.success('删除成功')
      })
    })
    .catch(() => {})
}
</script>

<style lang="scss" scoped>
.handle-btns {
  margin-bottom: 20px;
}
</style>
